package ru.ifmo.filemanager.filesystem.abstractplugin;

import static ru.ifmo.filemanager.async.Async.execute;

import java.io.IOException;

import ru.ifmo.filemanager.filesystem.DirectoryListNotifier;
import ru.ifmo.filemanager.filesystem.DirectoryListTask;
import ru.ifmo.filemanager.filesystem.DirectoryLookupNotifier;
import ru.ifmo.filemanager.filesystem.DirectoryLookupTask;
import ru.ifmo.filemanager.filesystem.Type;
import ru.ifmo.filemanager.filesystem.interfaces.Directory;
import ru.ifmo.filemanager.filesystem.interfaces.DirectoryListReceiver;
import ru.ifmo.filemanager.filesystem.interfaces.DirectoryLookupReceiver;
import ru.ifmo.filemanager.filesystem.interfaces.FileSystemObject;

public abstract class AbstractDirectory extends AbstractFileSystemObject implements Directory {
	
	public Type type() {
		return Type.DIRECTORY;
	}

	public String[] list() throws IOException {
		DirectoryListNotifier notifier = new DirectoryListNotifier();
		listAsync(notifier);
		return notifier.await();
	}

	public void listAsync(DirectoryListReceiver receiver) {
		execute(new DirectoryListTask(this, receiver));
	}

	public FileSystemObject lookup(String name) throws IOException {
		DirectoryLookupNotifier notifier = new DirectoryLookupNotifier();
		lookupAsync(name, notifier);
		return notifier.await();
	}

	public void lookupAsync(String name, DirectoryLookupReceiver receiver) {
		execute(new DirectoryLookupTask(this, name, receiver));
	}
}